package com.qire.manhua.model.viewModel.impl;

import android.app.Activity;
import android.view.View;
import android.widget.BaseAdapter;

import com.qire.common.basic.ObjectUtil;
import com.qire.common.support.AbsBringDialogViewModel;
import com.qire.common.support.base.IViewModel;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.adapter.auto.SummerBindAdapter;
import com.qire.common.widget.adapter.auto.annotation.AdapterEntityBind;
import com.qire.manhua.BR;
import com.qire.manhua.R;
import com.qire.manhua.controller.BuyCoinsMarketController;
import com.qire.manhua.dal.service.BuyCoinsMarketService;
import com.qire.manhua.databinding.LayoutBuyCoinsMarketItemBinding;
import com.qire.manhua.dialog.BuyResultTipsDialog;
import com.qire.manhua.dialog.BuyResultTipsDialog.PayTipType;
import com.qire.manhua.dialog.SpecialEventDialog;
import com.qire.manhua.model.entity.MarketInfo;
import com.qire.manhua.model.entity.MarketInfo.WaresEntity;
import com.qire.manhua.model.entity.OrderEntity;
import com.qire.manhua.model.enumeration.PaymentMethod;

import java.util.ArrayList;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;

public class BuyCoinsMarketViewModel extends AbsBringDialogViewModel implements IViewModel {

    private BuyCoinsMarketService marketService = new BuyCoinsMarketService(this);

    /** 充值选项列表 */
    private final ArrayList<WaresItemViewModel> waresItemList = new ArrayList<>();
    private final SummerBindAdapter marketAdapter = new SummerBindAdapter(waresItemList);

    private Activity activity;

    /** 营销市场信息：商品信息 */
    public MarketInfo marketInfo;
    /** 当前选择的商品信息 */
    private WaresItemViewModel selectedWaresViewModel;
    /** qq号码 */
    private String qqNumber = "";

    @Override
    public int getVariableId() {
        return BR.buyCoinsMarketViewModel;
    }

    //region    #附加管理 begin
    /**
     * 附加属性绑定
     * @param controller 事件控制器
     */
    public void onAddition(Activity activity, BuyCoinsMarketController controller) {
        this.activity = activity;
        marketAdapter.setEventProcess(controller);
    }

    /**
     * 清理函数，在Activity退出时，ViewModel生命周期到期会被回收，调用该事件，
     * 页面由于切换、横竖屏转换、内存回收等情况并不会影响到ViewModel的生命周期。
     */
    @Override
    protected void onCleared() {
        super.onCleared();
        activity = null;
        marketService = null;
        marketAdapter.setEventProcess(null);
    }
    //endregion #附加管理 end

    //region    #业务数据 begin

    public void loadPageData() {
        marketService.loadMarketInfo();
    }

    public void loadMarketInfo(MarketInfo marketInfo) {
        this.marketInfo = marketInfo;
        waresItemList.clear();
        for(WaresEntity waresEntity : marketInfo.waresEntityList) {
            waresItemList.add(new WaresItemViewModel(waresEntity.setProductId(OrderEntity.用户中心)));
        }
        marketAdapter.notifyDataSetChanged();
        waresItemList.get(0).toggle();
    }

    public void createOrder(PaymentMethod paymentMethod, String fromPayActivity) {
        if(ObjectUtil.nonNull(selectedWaresViewModel)) {
//            showDialog("请稍候...");
            marketService.createOrder(selectedWaresViewModel.buildOrderEntity(), paymentMethod, fromPayActivity);
        }
    }


    public void setQqNumber(String qqNumber) {
        this.qqNumber = qqNumber;
    }
    public String qqNumber() {
        return this.qqNumber;
    }

    private void selectWares(WaresItemViewModel selectedWaresViewModel) {
        this.selectedWaresViewModel = selectedWaresViewModel;
    }

    public WaresEntity selectedWares() {
        if(selectedWaresViewModel == null) {
            return null;
        }
        return selectedWaresViewModel.waresEntity;
    }

    //endregion #业务数据 end

    //region    #UI操作 begin

    public void showBuyResult(int orderCoin, int awardBean) {
        new BuyResultTipsDialog(activity, PayTipType.Recharge)
                .setWare(orderCoin, awardBean)
                .show();
    }

    public void showSpecialEvent(String imgUrl, String pageUrl) {
        new SpecialEventDialog(activity)
                .setImagePath(imgUrl)
                .setEventInfo(pageUrl, "").show();
    }

    //endregion #UI操作 end

    //region    #UI绑定 begin

    public BaseAdapter getMarketAdapter() {
        return marketAdapter;
    }

    //endregion #UI绑定 end

    @AdapterEntityBind(viewRid = R.layout.layout_buy_coins_market_item)
    public class WaresItemViewModel extends BaseObservable implements AutoVariable<LayoutBuyCoinsMarketItemBinding> {

        private final WaresEntity waresEntity;
        private boolean isSelect = false;

        public WaresItemViewModel(WaresEntity waresEntity) {
            this.waresEntity = waresEntity;
        }

        @Override
        public void bindVariable(LayoutBuyCoinsMarketItemBinding viewBind) {
            viewBind.setWaresItemViewModel(this);
        }

        public OrderEntity buildOrderEntity() {
            return waresEntity.buildOrderEntity(0);
        }

        public void toggle() {
            if(selectedWaresViewModel != null && selectedWaresViewModel != this) {
                selectedWaresViewModel.toggle();
            }
            this.isSelect = !isSelect;
            if(isSelect) {
                selectWares(this);
            } else {
                selectWares(null);
            }
            notifyChange();
        }

        @Bindable
        public boolean getChecked() {
            return isSelect;
        }
        @Bindable
        public CharSequence getWaresFee() {
            return waresEntity.fee + "元";
        }
        @Bindable
        public CharSequence getWaresDescribe() {
            return waresEntity.getDescribe();
        }
        @Bindable
        public int getWaresRewardFeeVisible() {
            return (waresEntity.rewardFee > 0) ? View.VISIBLE : View.GONE;
        }
        @Bindable
        public CharSequence getWaresRewardFee() {
            return (waresEntity.rewardFee > 0) ? "送" + waresEntity.getRewardRate() + "%" : "";
        }
        @Bindable
        public int getFirstPayBuoyVisible() {
            if(waresEntity.rewardFee <= 0) {
                return View.GONE;
            }
            return marketInfo.isFirstPay ? View.VISIBLE : View.GONE;
        }

    }

}
